package org.lgiavedoni.android.franq.event.generation.phone;

import org.lgiavedoni.android.franq.core.bean.SMS;
import org.lgiavedoni.android.franq.core.listener.PhoneBroadcastListener;
import org.lgiavedoni.android.franq.event.SMSFromEvent;
import org.lgiavedoni.android.franq.service.event.EventService;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SMSFromListener extends PhoneBroadcastListener {

	private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

	@Override
	public IntentFilter getListenetFilter() {
		return new IntentFilter(SMS_RECEIVED);
	}

	@Override
	public void onReceive(Context paramContext, Intent paramIntent) {
		// ---get the SMS message passed in---
		Bundle bundle = paramIntent.getExtras();
		SmsMessage[] msgs = null;
		if (bundle != null) {
			// ---retrieve the SMS message received---
			Object[] pdus = (Object[]) bundle.get("pdus");
			msgs = new SmsMessage[pdus.length];
			for (int i = 0; i < msgs.length; i++) {
				msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

				SMSFromEvent e = new SMSFromEvent(new SMS(msgs[i].getOriginatingAddress(), msgs[i].getMessageBody()));
				EventService.inst.fireEvent(e);
			}

		}
	}

}
